<?

class Settings extends CI_Controller {

	public function __construct()
	{
		parent::__construct();

		if(!$this->auth->is_admin() && !$this->auth->is_super_admin())
			$this->auth->verify();

		$this->load->model('user_model');

		$this->load->helper('file');
		$this->load->helper('download');
	}

	public function index()
	{
		$data['page'] = 'admin/settings';
		$this->load->view('admin/index', $data);
	}

	public function download_db()
	{
		$backup = generate_db();
		force_download('pos-sys-db('.date('Y-m-d').').sql', $backup);
	}

	public function email_db()
	{
		$this->load->helper('file');

		$backup = generate_db();
		write_file('html/db_backup/pos-sys-db('.date('Y-m-d').').sql' , $backup);

		$message = 'The copy of the database is attached to this email.';
		$user_email = $this->user_model->get_user_by_id($this->session->userdata['user_id'])->user_email;
		$this->email->attach('html/db_backup/pos-sys-db('.date('Y-m-d').').sql');
		send_email(
				$user_email,
				'A Cycle House Database Backup',
				$message
			);
	}
}